
#include <manager.h>

void *rp_rindexd_manager(void *arg)
{
	char fname[] = "rp_rindexd_manager";
	char msg[] = "fatal error";
	Err err;
	int fd;
	int tbc = 1;
	Cmd cmd;
	FLOG_IN(fname);

	while (tbc)			// set tbc = 0 to break from the While loop
	{
		/*	block waiting for the incoming command    */
		err = rp_sem_wait(&(global.admq.sem));
		fd = rp_admq_get(&(global.admq));
		memset(&cmd, 0, sizeof(Cmd));
		if (rp_comm_recv(fd, (void *)&cmd, sizeof(Cmd)) <  sizeof(Cmd))
		{
			rp_log(LOG_ERROR, fname, "Cannot receive the incoming command.");	
			continue;
		}
		/*	process the command    */
		switch (cmd.code)
		{
			case (OP_SHUTDOWN) : {
				global.code = OP_SHUTDOWN;
				break;
			}
			case (OP_SHUTDOWN_FORCE) : {
				global.code = OP_SHUTDOWN_FORCE;
				break;
			}
			case (OP_PAUSE) : {
				global.code = OP_PAUSE;
				break;
			}
			case (OP_RESUME) : {
				global.code = OP_NONE;		// Shall we resume immediately?
				break;
			}
			case (OP_REPLICA) : {
				break;
			}
			case (OP_NONE) : {				// Why do we have this?
				rp_log(LOG_WARNING, fname, "Are you kidding me?");
				break;
			}
			default : {
				rp_log(LOG_WARNING, fname, "Invalid command");
			}
		}
	}
	
	FLOG_OUT(fname);
	return NULL;
}

